package com.xhwl.centiotdata.service.integrateconfig;

import com.xhwl.centiotdata.pojo.dto.brodcast.PhonogramConfigDTO;
import com.xhwl.common.enums.CategoryEnum;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;

/**
 * @author pengjunming
 * @description
 * @date 2022/10/8 10:21
 */
@Component
public class PhonogramIntegrationConfig extends IntegrationConfig<Integer, List<PhonogramConfigDTO>> {

    private Map<Integer, List<PhonogramConfigDTO>> phonogramConfigMap = new ConcurrentHashMap<>();

    @Override
    public List<PhonogramConfigDTO> getIntegrationConfig(Integer projectId) {
        return phonogramConfigMap.get(projectId);
    }

    public Map<Integer, List<PhonogramConfigDTO>> getIntegrationConfig() {
        return phonogramConfigMap;
    }

    @EventListener
    public void listenIntegrationConfig(IntegrationConfigEvent event) {
        this.handlePhonogramIntegrationConfig();
    }

    private void handlePhonogramIntegrationConfig() {
        phonogramConfigMap = IntegrationConfig.integrationConfigList.stream()
                .filter(deviceProjSupplierCategory -> CategoryEnum.PHONOGRAM_SYSTEM.getId().equals(deviceProjSupplierCategory.getProdCategoryId()))
                .map(PhonogramConfigDTO::toPhonogramConfig)
                .collect(Collectors.groupingByConcurrent(PhonogramConfigDTO::getProjectId));

    }

}